package com.ultra.spider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.ultra.util.HttpsUtils;
import com.ultra.util.PropertyUtil;



public class DayData {
	private static final Logger logger = Logger.getLogger(DayData.class);
	public static void main(String[] args) {
		try {
			//程序开始时间
            logger.info("程序运行中...");
            long startTime = System.currentTimeMillis();
            Map<String, Object> map4 = new HashMap<>();
			List<Map<String, Object>> list2 = new ArrayList<>();
			Properties property = PropertyUtil.loadProps2("/city.properties");
		    Set<Object> keyset = property.keySet();  
            Iterator<Object> itr = keyset.iterator();
            String key = "";
            String param1 = "";
            String param2 = "";
            while(itr.hasNext()){
            	key = (String) itr.next();
            	param1 = property.getProperty(key).split(",")[0];
            	param2 = property.getProperty(key).split(",")[1];
            	String url = "https://www.cwb.gov.tw/V7/forecast/taiwan/"+param1;
                String	post = HttpsUtils.post(url, null, null, null);
    			Document document = Jsoup.parse(post);
    			String url2 = "https://www.cwb.gov.tw/V7/forecast/taiwan/Data/"+param2;
    			String post2 = HttpsUtils.post(url2, null, null, null);
    			Document document2 = Jsoup.parse(post2);
    			Map<String, Object> map3= new HashMap<>();
    			String text = document2.select("body").text();
    			String time = text.substring(text.length()-11,text.length());
    			String describe = text.substring(0,text.length()-16);
    			map3.put("publishtime", time);
    			map3.put("describe", describe);
    			Map<String, Object> map2 = new HashMap<>();
    			String city = document.select("select").select("[selected='selected']").get(0).text();
    			map2.put("city", city);
    			map2.put("live", map3);
    			String publishTime = document.select(".Issued").get(0).text();
    			publishTime = publishTime.substring(7, publishTime.length());
    			Map<String, Object> map1 = new HashMap<>();
    			map1.put("publishtime", publishTime);
    			Elements tableList = document.select(".FcstBoxTable01");
    			Elements trList = tableList.get(0).select("tr");
    			List<Map<String, Object>> list = new ArrayList<>();
    			for(int i=1;i<trList.size();i++){
    				Elements thList = trList.get(i).select("th");
    				Elements tdList = trList.get(i).select("td");
    				Map<String, Object> map = new HashMap<>();
    				map.put("datetime", thList.get(0).text());
    				map.put("temperature", tdList.get(0).text());
    				map.put("weather-regime", tdList.get(1).select("img").attr("alt"));
    				map.put("comfort", tdList.get(2).text());
    				map.put("Precipitation-probability", tdList.get(3).text());
    				list.add(map);
    			}
    			map1.put("data", list);
    			map2.put("forecast", map1);
    			list2.add(map2);
            }
            map4.put("data", list2);
            String jsonString = JSON.toJSONString(map4);
            System.out.println(jsonString);
            //程序结束时间
	        long endTime = System.currentTimeMillis();    
	        logger.info("程序运行时间：" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("运行出错:===============================>"+e.getMessage());
		}
	}
}
